// packageF/crowd_activity/funding/funding.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payShow: false,
    allUserShow: false,
    infoData: [],
    // 全部报名人员
    allUserShowList: [],
    allUserpage: 1,
    // 支持的固定金额
    payPrice: [],
    diyPayprice: "",
    priceIndex: null,
    // 海报
    posterShow: false,
    poster_Data: {
      poster_data: null,
      background: {}
    },
    poster_id: null, //海报id
    iPnoneBottomBol: false,
    showcarrfootBol: true,
    isNewPosterPlugin: false, //是否为新海报
    Hei:0,
    imgCurrent:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.data.options = options;
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene, 'dddddd')
      var info_arr = [];
      info_arr = scene.split(',');
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split('=');
        if (chil_arr[0] == "id") {
          this.data.options.id = chil_arr[1];
        }
        if (chil_arr[0] == "mid") {
          app._setMid(chil_arr[1])
        }
      }
    }
    this.init();
    this.getData();
    this.getiPnoneBottom();
  },
  swiperChange(e){
    let {current} = e.detail;
    this.setData({
      imgCurrent:current + 1
    })
  },
  imgH(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width;
    var swiperH = (winWid * imgh / imgw )-20   //等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (this.data.Hei < swiperH) {
      this.setData({
        Hei: swiperH //设置高度
      })
    }
  },
  toActiveGood() {
    wx.navigateTo({
      url: '/packageF/crowd_activity/goods/goods?id=' + this.data.infoData.activity_id,
    })
  },
  getiPnoneBottom() {
    let that = this;
    try {
      var value = wx.getStorageSync("iPhoneBottom");
      if (value) {
        that.setData({
          iPnoneBottomBol: true,
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  init() {
    this.setData({
      posterShow: false,
      payShow: false,
      allUserShow: false
    })
  },
  toHome(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  onChange(e) {
    // console.log(e);
    this.setData({
      timeData: e.detail,
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.activity-member.show");
    app._getNetWork({
      url: urlStr,
      data: {
        activity_member_id: this.data.options.id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            infoData: res.data
          })
          let sj = Date.now();
          this.setData({
            'infoData.belongs_to_activity.start_at':Number(res.data.belongs_to_activity?.start_at+'000') - sj,
            'infoData.belongs_to_activity.end_at':Number(res.data.belongs_to_activity?.end_at+'000') - sj
          })
          wx.setNavigationBarTitle({
            title: this.data.infoData.belongs_to_activity.title ? `${this.data.infoData.current_activity_member.nickname}的${this.data.infoData.belongs_to_activity.title}` : '众筹活动',
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  toActive() {
    wx.navigateTo({
      url: '/packageF/crowd_activity/goods/goods?id=' + this.data.infoData.activity_id,
    })
  },
  allUserComfirm() {
    // 支持金额弹窗确认
    let price = "";
    if (this.data.priceIndex == 0 || this.data.priceIndex) {
      price = this.data.payPrice[this.data.priceIndex];
    } else {
      price = this.data.diyPayprice;
    }
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.pre-activity-order.crowdfunding");
    app._getNetWork({
      url: urlStr,
      data: {
        activity_member_id: this.data.infoData.id,
        amount: price
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let sn = res.data.order_sn
          this.setData({
            payShow: false,
          })
          wx.navigateTo({
            url: '/packageF/crowd_activity/crowd_activity_OrderPay/crowd_activity_OrderPay?sn=' + sn,
          })
        } else {
          this.setData({
            payShow: false
          })
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  tapPriceSelect(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      PaypriceOnece: item,
      priceIndex: index
    })
  },
  tapPayShowInp(e) {
    let val = e.detail.value;
    this.setData({
      diyPayprice: val,
      priceIndex: null
    })
  },
  payShowTap() {
    this.setData({
      payShow: true,
      payPrice: [],
      diyPayprice: "",
      priceIndex: null
    })
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.activity.fixedAmount");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            payPrice: res.data
          })
          if (res.data.length > 0) {
            this.setData({
              priceIndex: 0
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  posterShowTap() {
    this.setData({
      posterShow: true
    })
  },
  allUserShowTap() {
    this.setData({
      allUserShow: true
    })
  },
  allUserShowCancel() {
    this.setData({
      allUserShow: false
    })
  },
  cancelPayShow() {
    this.setData({
      payShow: false
    })
  },
  tapPayShow() {
    this.setData({
      payShow: true
    })
  },
  confirmPayShow() {
    // 支持金额确认按钮方法
  },
  tapPosterShow() {
    this.setData({
      posterShow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.infoData.belongs_to_activity.title ? `${this.data.infoData.current_activity_member.nickname}的${this.data.infoData.belongs_to_activity.title}` : '众筹活动',
      path: `/packageF/crowd_activity/funding/funding?id=${this.data.options.id}&mid=${mid}`,
      imageUrl:this.data.infoData.belongs_to_activity?.image_url?this.data.infoData.belongs_to_activity?.image_url:''
    }
  },
  onShareTimeline(){
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return{
      title: this.data.infoData?.belongs_to_activity?.title ? `${this.data.infoData.current_activity_member.nickname}的${this.data.infoData?.belongs_to_activity?.title}` : '众筹活动',
      query: `id=${this.data.options.id}&mid=${mid}`,
      imageUrl: this.data.infoData.belongs_to_activity?.image_url?this.data.infoData.belongs_to_activity?.image_url:'',
    }
  }
})